﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace APRIR
{
    public partial class MainForm : Form
    {
        System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string[] PortName = System.IO.Ports.SerialPort.GetPortNames();  // 获取串口名字
            TSMPortName.Items.AddRange(PortName);
            TSMPortName.SelectedIndex = 0;
            serialPort.Parity = System.IO.Ports.Parity.Even;
            this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
        }

        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (serialPort.BytesToRead > 0)
            {
                string strRev =serialPort.ReadExisting();// serialPort.ReadLine(); 
                CommandPrase(strRev);
            }
        }

        private void CommandPrase(string strRev)
        {
            this.lsbShowInfo.Items.Add("recv:"+strRev);
            switch (strRev)
            {
                case "#84@00B8A5\r":
                   // this.lsbShowInfo.Items.Add(DateTime.Now.ToString("HH:mm:ss") + "> Command Completed");
                    serialPort.WriteLine("#84@029BB7\r\n");
                    break;
                case "#8A@008CB7\r":
                   // this.lsbShowInfo.Items.Add(DateTime.Now.ToString("HH:mm:ss") + "> Transponder successfully tested");
                    break;
                case "#90BC66\r":
                   // this.lsbShowInfo.Items.Add(DateTime.Now.ToString("HH:mm:ss") + "> Transponder is empty");
                    break;
                case "#8411B3A\r":
                   // this.lsbShowInfo.Items.Add(DateTime.Now.ToString("HH:mm:ss") + "> DD");
                    serialPort.WriteLine("#84@00B8A5\r\n");
                    break;
                case "#8AAMTECH              F007\r":
                    serialPort.WriteLine("#8A@008CB7\r");
                    break;
                case "#8433828\r":
                    serialPort.WriteLine("#84@00B8A5\r\n");
                    break;
                default:
                    this.lsbShowInfo.Items.Add(strRev);
                    break;
            }
            
        }

        private void TSMPortName_Click(object sender, EventArgs e)
        {
        
        }

        private void TSMPortName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
                serialPort.PortName = TSMPortName.SelectedItem.ToString();
                serialPort.Open();
                
            }
            catch
            {
            }
            finally
            {

            }
        }
    }
}
